# -*- encoding:utf-8 -*-
# __author__ = 'chenyongbing'
# __poject__ = 'ops_platform'
# __datetime__ = '2022/5/12'
# __name__ = 'filters.py'
from django_filters.rest_framework import FilterSet
import django_filters

from .models import *
# Create your code here.


class CapacityFilter(FilterSet):
    """
    支持模糊搜索
    icontains，包含且忽略大小写
    """
    name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')
    class Meta:
        # 指定模型
        models = Capacity
        # 指定需要模糊查询的字段
        fields = ['name']

class YAMLTemplateFilter(FilterSet):
    """
        支持模糊搜索
        icontains，包含且忽略大小写
        """
    name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')

    class Meta:
        # 指定模型
        models = YAMLTemplate
        # 指定需要模糊查询的字段
        fields = ['name']
        
        
# class BusinessEnvSettingFilter(FilterSet):
#     """
#         支持模糊搜索
#         icontains，包含且忽略大小写
#         """
#     # business_id = django_filters.CharFilter(field_name='business_id')
#     business = django_filters.CharFilter(field_name='business')
# 
#     class Meta:
#         # 指定模型
#         models = BusinessEnvSetting
#         # 指定需要模糊查询的字段
#         fields = ['business']
# 
# class ProjectEnvSettingFilter(FilterSet):
#     """
#         支持模糊搜索
#         icontains，包含且忽略大小写
#         """
#     # business_id = django_filters.CharFilter(field_name='business_id')
#     # project_id = django_filters.CharFilter(field_name='project_id')
#     business = django_filters.CharFilter(field_name='business')
# 
#     project = django_filters.CharFilter(field_name='project')
# 
#     class Meta:
#         # 指定模型
#         models = ProjectEnvSetting
#         # 指定需要模糊查询的字段
#         fields = ['business', 'project']
# 
# class ServiceEnvSettingFilter(FilterSet):
#     """
#         支持模糊搜索
#         icontains，包含且忽略大小写
#         """
#     # business_id = django_filters.CharFilter(field_name='business_id')
#     # project_id = django_filters.CharFilter(field_name='project_id')
#     # service_id = django_filters.CharFilter(field_name='service_id')
#     business = django_filters.CharFilter(field_name='business')
#     project = django_filters.CharFilter(field_name='project')
#     service = django_filters.CharFilter(field_name='service')
#     class Meta:
#         # 指定模型
#         models = ServiceEnvSetting
#         # 指定需要模糊查询的字段
#         fields = ['business', 'project', 'service']

class LobEnvSettingFilter(FilterSet):
    """
        支持模糊搜索
        icontains，包含且忽略大小写
        """

    # def __init__(self, data=None, *args, **kwargs):
    #     # if filterset is bound, use initial values as defaults
    #     if data is not None:
    #         # get a mutable copy of the QueryDict
    #         data = data.copy()
    #
    #         for name, f in self.base_filters.items():
    #             initial = f.extra.get('initial')
    #
    #             # filter param is either missing or empty, use initial as default
    #             if not data.get(name) and initial:
    #                 data[name] = initial
    #
    #     super().__init__(data, *args, **kwargs)

    # @property
    # def qs(self):
    #     parent = super().qs
    #     query_params = self.data
    #     business_id = getattr(dict(self.data), 'business_id', None)
    #     project_id = getattr(dict(self.data), 'project_id', None)
    #     service_id = getattr(dict(self.data), 'service_id', None)
    #     if service_id and project_id and business_id:
    #         return parent.filter(service_id=service_id)
    #     elif project_id and business_id:
    #         return parent.filter(project_id=project_id)
    #     else:
    #         return parent.filter(business_id=business_id)



    # business_id = django_filters.CharFilter(field_name='business_id', method='filter_business_null')
    # project_id = django_filters.CharFilter(field_name='project_id', method='filter_project_null')
    # service_id = django_filters.CharFilter(field_name='service_id', method='filter_service_null')
    business_id = django_filters.CharFilter(field_name='business_id', method='filter_business_null')
    project_id = django_filters.CharFilter(field_name='project_id', method='filter_project_null')
    service_id = django_filters.CharFilter(field_name='service_id', method='filter_service_null')
    class Meta:
        # 指定模型
        models = LobEnvSetting
        # 指定需要模糊查询的字段
        fields = ['business_id', 'project_id', 'service_id']

    def filter_business_null(self, queryset, field, value):
        # do somethings
        return queryset.filter(**{field: value, 'project_id__isnull': True, 'service_id__isnull': True})

    def filter_project_null(self, queryset, field, value):
        # do somethings
        return queryset.filter(**{field: value, 'service_id__isnull': True})

    def filter_service_null(self, queryset, field, value):
        # do somethings
        return queryset.filter(**{field: value})

class WorkOrderFilter(FilterSet):
    """
        支持模糊搜索
        icontains，包含且忽略大小写
        """

    demand = django_filters.CharFilter(field_name='demand')


    class Meta:
        # 指定模型
        models = WorkOrder
        # 指定需要模糊查询的字段
        fields = ['demand']